package handler

import (
	"github.com/gin-gonic/gin"
	"red-book-manager/model"
	"red-book-manager/pkg"
)

func Login(c *gin.Context) {
	username := c.PostForm("username")
	password := c.PostForm("password")
	if username == "" || password == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}
	data := model.User{

		Username: username,
		Password: password,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(400, gin.H{
			"code": 400,
			"msg":  "登录失败",
		})
		return
	}
	if password != pkg.MD5(password) {
		c.JSON(400, gin.H{
			"code": 500,
			"msg":  "登录失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "登录成功",
	})
	return
}
func List(c *gin.Context) {
	var list []model.User
	err := model.DB.Find(&list).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "查询失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return
}
